package io.vov.vitamio.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.ViewGroup;

public class TVFragmentPagerAdapter extends FragmentStatePagerAdapter {
    private FragmentManager mManager;
    private FragmentTransaction ft;
    private String[] datas;
    private int mPosition;
	public TVFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    public TVFragmentPagerAdapter(FragmentManager fm, String[] mTabTitle) {
        super(fm);
        mManager = fm;
        this.datas = mTabTitle;
//        setFragments();                                                                 
    }
    public TVFragmentPagerAdapter(FragmentManager supportFragmentManager, String[] mTabTitle, int position) {
		super(supportFragmentManager);
		this.datas = mTabTitle;
        mManager = supportFragmentManager;
        mPosition = position;

	}
    
//	public void setFragments() {
//		if(this.fragmentsList != null){
//			FragmentTransaction ft = mManager.beginTransaction();
//			for(Fragment f:this.fragmentsList){
//				ft.remove(f);
//			}
//			ft.commit();
//			ft=null;
//			mManager.executePendingTransactions();
//		}
//		notifyDataSetChanged();
//	}
//    


	
	@Override
    public int getCount() {
        return datas == null ? 0 : datas.length;
    }

    @Override
    public Fragment getItem(int index) {
    	Log.e("TVFragmentPagerAdapter>>>", datas[index]);
    	TVFragment fragment = new TVFragment();
    	fragment.setData(datas[index],mPosition);
    	ft = mManager.beginTransaction();
    	ft.addToBackStack(datas[index]);
    	ft.commitAllowingStateLoss();
//    	fragmentsList.add(fragment);
        return fragment;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }
    
    
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    	return super.instantiateItem(container, position);
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    	super.destroyItem(container, position, object);
    }

}
